package com.qc.wintrax.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.qc.wintrax.R;
import com.qc.wintrax.utils.ImageLoader;

public class ViewHolder {
    private final SparseArray<View> mViews;
    private int mPosition;
    private View mConvertView;

    private ViewHolder(Context context, ViewGroup parent, int layoutId,
                       int position) {
        this.mPosition = position;
        this.mViews = new SparseArray<View>();
        mConvertView = LayoutInflater.from(context).inflate(layoutId, parent,
                false);
        // setTag
        mConvertView.setTag(this);
    }

    /**
     * 拿到一个ViewHolder对象
     *
     * @param context
     * @param convertView
     * @param parent
     * @param layoutId
     * @param position
     * @return
     */
    public static ViewHolder get(Context context, View convertView,
                                 ViewGroup parent, int layoutId, int position) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder(context, parent, layoutId, position);
        } else {
            holder = (ViewHolder) convertView.getTag();
            holder.mPosition = position;
        }
        return holder;
    }

    public View getConvertView() {
        return mConvertView;
    }

    /**
     * 通过控件的Id获取对于的控件，如果没有则加入views
     *
     * @param viewId
     * @return
     */
    public <T extends View> T getView(int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    /**
     * 为TextView设置字符串
     *
     * @param viewId
     * @param text
     * @return
     */
    public ViewHolder setText(int viewId, String text) {
        TextView view = getView(viewId);
        view.setText(text);
        return this;
    }

    /**
     * 为TextView设置字符串AND颜色
     *
     * @param viewId
     * @param text
     * @return
     */
    public ViewHolder setText(int viewId, String text , int viewIdColor) {
        TextView view = getView(viewId);
        view.setText(text);
        view.setTextColor(viewIdColor);
        return this;
    }
    /**
     * 设置checkBox的选中状态
     *
     */
    public ViewHolder setChecked(int viewId,boolean checked){
        CheckBox view = getView(viewId);
        view.setChecked(checked);
        return this;
    }

    /**
     * 为ImageView设置显示
     *
     * @param viewId
     * @param 'text
     * @return
     */
    public ViewHolder setImageVisible(int viewId) {
        ImageView view = getView(viewId);
        view.setVisibility(View.VISIBLE);
        return this;
    }

    /**
     * 为ImageView设置隐藏
     *
     * @param viewId
     * @param 'text
     * @return
     */
    public ViewHolder setImageGone(int viewId) {
        ImageView view = getView(viewId);
        view.setVisibility(View.GONE);
        return this;
    }

    /**
     * 为TextView设置显示
     *
     * @param viewId
     * @param 'text
     * @return
     */
    public ViewHolder setTextVisible(int viewId) {
        TextView view = getView(viewId);
        view.setVisibility(View.VISIBLE);
        return this;
    }


    /**
     * 为TextView设置隐藏
     *
     * @param viewId
     * @param 'text
     * @return
     */
    public ViewHolder setTextGone(int viewId) {
        TextView view = getView(viewId);
        view.setVisibility(View.GONE);
        return this;
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @param drawableId
     * @return
     */
    public ViewHolder setImageResource(int viewId, int drawableId) {
        ImageView view = getView(viewId);
        view.setImageResource(drawableId);

        return this;
    }
    public ViewHolder setBackgroundResource(int viewId, int viewIdColor) {
        LinearLayout view = getView(viewId);
        view.setBackgroundResource(viewIdColor);
        return this;
    }

    /**
     * 为ImageView设置网络图片
     *
     * @param viewId
     * @param 'drawableId
     * @return
     */
    public ViewHolder setImageByUrlNet(int viewId, String url) {
        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(url, (ImageView) getView(viewId));
        return this;
    }

//    public DisplayImageOptions imageOptions(){
//        DisplayImageOptions options = new DisplayImageOptions.Builder().showImageForEmptyUri(R.mipmap.moren_touxiang_xiao)
//                .showImageForEmptyUri(R.mipmap.moren_touxiang_xiao)
//                .showImageOnFail(R.mipmap.moren_touxiang_xiao)
//                .cacheInMemory(true)
//                .cacheOnDisk(true)
//                .build();
//        return options;
//    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @param 'drawableId
     * @return
     */
    public ViewHolder setImageBitmap(int viewId, Bitmap bm) {
        ImageView view = getView(viewId);
        view.setImageBitmap(bm);
        return this;
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @param 'drawableId
     * @return
     */
    public ViewHolder setImageByUrl(int viewId, String url) {
        com.qc.wintrax.utils.ImageLoader.getInstance(3, ImageLoader.Type.LIFO).loadImage(url, (ImageView) getView(viewId));
        return this;
    }

    /**
     * 关于事件的
     */
    public ViewHolder setOnClickListener(int viewId,
                                         View.OnClickListener listener) {
        View view = getView(viewId);
        view.setOnClickListener(listener);
        return this;
    }

    public ViewHolder setOnTouchListener(int viewId,
                                         View.OnTouchListener listener) {
        View view = getView(viewId);
        view.setOnTouchListener(listener);
        return this;
    }

    public ViewHolder setOnLongClickListener(int viewId,
                                             View.OnLongClickListener listener) {
        View view = getView(viewId);
        view.setOnLongClickListener(listener);
        return this;
    }


    public int getPosition() {
        return mPosition;
    }


}
